/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;


import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Expression;


/**
 * An if-then-else statement, such as <code>if x { foo() }</code>.
 */
public class IfStatement extends Statement {
	
	
	/**
	 * The conditional expression to test for truth.
	 */
	private Expression test;
	
	
	/**
	 * The statement to execute if the test is true.
	 */
	private Statement positive;
	
	
	/**
	 * The statement to execute if the test is false.
	 */
	private Statement negative;


	/**
	 * Constructs a new if statement.
	 * 
	 * @param place  the place where the if statement originated
	 * @param test   the test expression
	 * @param positive  the statement to execute if the test is true
	 * @param negative  the statement to execute if the test is false;
	 *   may be null if there is no negative path
	 */
	public IfStatement(Place place, Expression test, Statement positive, Statement negative) {
		super(place);
		if (test == null) {
			throw new IllegalArgumentException();
		}
		if (positive == null) {
			throw new IllegalArgumentException();
		}
		this.test = test;
		this.positive = positive;
		this.negative = negative;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("if ").append(test).append(" ").append(positive);
		if (negative != null) {
			sb.append(" else ").append(negative);
		}
		return sb.toString();
	}

	
	/**
	 * Returns the statement to execute if the test is false.
	 * May be null if there is no negative branch.
	 * 
	 * @return  the negative branch statement
	 */
	public Statement getNegativeBranch() {
		return negative;
	}

	
	/**
	 * Returns the statement to execute if the test is true.
	 * 
	 * @return  the positive branch statement
	 */
	public Statement getPositiveBranch() {
		return positive;
	}

	
	/**
	 * Returns the expression to test.
	 * This should evaluate to a boolean type.
	 * 
	 * @return  the test expression
	 */
	public Expression getTest() {
		return test;
	}


	@Override
	public boolean terminates() {
		if (negative != null) {
			return negative.terminates() && positive.terminates();
		} else {
			return positive.terminates();
		}
	}
	

}
